package com.mall.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.constant.MessageConstant;
import com.mall.common.exception.GlobalException;
import com.mall.common.utils.JwtUtils;
import com.mall.user.entity.dto.UserLoginDTO;
import com.mall.user.entity.po.User;
import com.mall.user.entity.vo.UserLoginVO;
import com.mall.user.enums.UserStatus;
import com.mall.user.mapper.UserMapper;
import com.mall.user.service.UserService;
import com.mall.user.utils.PasswordUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    /*
     * 用户登录
     * */
    @Override
    @Transactional
    public UserLoginVO login(UserLoginDTO dto) {
        //1.正常登录 手机号和密码
        if (StringUtils.isNoneBlank(dto.getPhone()) && StringUtils.isNoneBlank(dto.getPassword())){
            //1.1根据手机号查询用户信息
            User dbUser = getOne(Wrappers.<User>lambdaQuery().eq(User::getPhone, dto.getPhone()));
            if (dbUser == null){
                throw new GlobalException(MessageConstant.ACCOUNT_NOT_FOUND);
            }
            //1.2比对密码
            String password = dto.getPassword();
            String salt = dbUser.getPassword();
            if (!PasswordUtil.verifyPassword(password,salt)){
                throw new GlobalException(MessageConstant.PASSWORD_ERROR);
            }
            //1.3返回数据  jwt
            Map<String, Object> claims = new HashMap<>();
            claims.put("id",dbUser.getId());
            String jwt = JwtUtils.generateToken(claims);
            return new UserLoginVO(dbUser.getId(),dbUser.getNickname(),jwt);
        } else {
        //2.游客登录
            Map<String, Object> claims = new HashMap<>();
            claims.put("nickname","游客登录");
            String jwt = JwtUtils.generateToken(claims);
            return new UserLoginVO(null,"游客登录",jwt);
        }
    }
    /*
    * 用户注册
    * */
    @Override
    @Transactional
    public void register(UserLoginDTO dto) {
        String phone = dto.getPhone();
        String password = dto.getPassword();
        // 用正则表达式匹配手机号格式
        /*if (!phone.matches("^1[3-9]\\d{9}$")){
            throw new GlobalException("手机号格式错误");
        }*/
        //判断手机号是否重复
        List<User> list = lambdaQuery().like(phone != null, User::getPhone, phone).list();
        if (!list.isEmpty()){
            throw new GlobalException(phone + "已注册过");
        }
        //构建用户
        User user = User.builder().status(UserStatus.NoRMAL)
                .password(PasswordUtil.hashPassword(password))  // 对密码加密处理
                .phone(phone)
                .build();
        boolean saveResult = this.save(user);
        if (!saveResult){
            throw new GlobalException("注册失败，请稍后重试");
        }
    }
}
